# Generate vulnerability detector library
file(GLOB vulndetector_files
    ${SRC_FOLDER}/wazuh_modules/*.o
    ${SRC_FOLDER}/wazuh_modules/vulnerability_detector/*.o)
list(REMOVE_ITEM vulndetector_files ${SRC_FOLDER}/wazuh_modules/main.o)

add_library(VULNDETECTOR_O STATIC ${vulndetector_files})

set_source_files_properties(
    ${vulndetector_files}
    PROPERTIES
    EXTERNAL_OBJECT true
    GENERATED true
)

set_target_properties(
    VULNDETECTOR_O
    PROPERTIES
    LINKER_LANGUAGE C
)

target_link_libraries(VULNDETECTOR_O ${WAZUHLIB} ${WAZUHEXT} -lpthread)

# Generate vulnerability detector tests
list(APPEND vulndetector_names "test_wm_vuln_detector")
list(APPEND vulndetector_flags "-Wl,--wrap,_merror -Wl,--wrap,_mterror -Wl,--wrap,_mtdebug1 -Wl,--wrap,_mtdebug2 -Wl,--wrap,OS_ConnectUnixDomain \
                                -Wl,--wrap,OS_SendSecureTCP -Wl,--wrap,OS_RecvSecureTCP -Wl,--wrap,close -Wl,--wrap,sqlite3_prepare_v2 \
                                -Wl,--wrap,sqlite3_bind_text -Wl,--wrap,sqlite3_bind_int -Wl,--wrap,sqlite3_step -Wl,--wrap,sqlite3_close_v2 \
                                -Wl,--wrap,sqlite3_finalize -Wl,--wrap,sqlite3_errmsg -Wl,--wrap,cJSON_CreateObject -Wl,--wrap,cJSON_CreateArray \
                                -Wl,--wrap,OPENSSL_init_ssl -Wl,--wrap,OPENSSL_init_crypto -Wl,--wrap,StartMQ -Wl,--wrap,SendMSG -Wl,--wrap,strerror \
                                -Wl,--wrap,_mterror_exit -Wl,--wrap,OSHash_Begin -Wl,--wrap,sqlite3_column_text -Wl,--wrap,sqlite3_column_double \
                                -Wl,--wrap,cJSON_Delete -Wl,--wrap,cJSON_AddItemToObject -Wl,--wrap,cJSON_AddStringToObject -Wl,--wrap,cJSON_CreateNumber \
                                -Wl,--wrap,cJSON_CreateString -Wl,--wrap,cJSON_AddItemToArray -Wl,--wrap,cJSON_PrintUnformatted -Wl,--wrap,OSHash_Next \
                                -Wl,--wrap,pkg_version_relate -Wl,--wrap,cJSON_Parse -Wl,--wrap,cJSON_GetObjectItem -Wl,--wrap,OSHash_Add \
                                -Wl,--wrap,OSHash_Get -Wl,--wrap,_mtwarn -Wl,--wrap,sqlite3_column_int -Wl,--wrap,wm_vuldet_win_nvd_vulnerabilities \
                                -Wl,--wrap,OSHash_Create -Wl,--wrap,OSHash_setSize -Wl,--wrap,OSHash_Clean -Wl,--wrap,wm_vuldet_linux_nvd_vulnerabilities \
                                -Wl,--wrap,_mdebug1 -Wl,--wrap,_mdebug2 -Wl,--wrap,time -Wl,--wrap,wstr_end -Wl,--wrap,w_uncompress_gzfile \
                                -Wl,--wrap,json_fread -Wl,--wrap,remove -Wl,--wrap,fopen -Wl,--wrap,fclose -Wl,--wrap,wurl_request -Wl,--wrap,sleep \
                                -Wl,--wrap,sqlite3_open_v2 -Wl,--wrap,bzip2_uncompress -Wl,--wrap,fgets -Wl,--wrap,fwrite -Wl,--wrap,localtime \
                                -Wl,--wrap,OS_ReadXML -Wl,--wrap,OS_GetElementsbyNode -Wl,--wrap,OSRegex_Compile -Wl,--wrap,OSRegex_Execute \
                                -Wl,--wrap,w_get_file_content -Wl,--wrap,wm_vuldet_json_nvd_parser -Wl,--wrap,wm_vuldet_json_wcpe_parser \
                                -Wl,--wrap,wm_vuldet_json_msu_parser -Wl,--wrap,opendir -Wl,--wrap,closedir -Wl,--wrap,readdir -Wl,--wrap,w_is_file")

list(APPEND vulndetector_names "test_wm_vuln_detector_evr")
list(APPEND vulndetector_flags "-Wl,--wrap,_merror -Wl,--wrap,_mterror -Wl,--wrap,_mtdebug1 -Wl,--wrap,_mtdebug2 -Wl,--wrap,OS_ConnectUnixDomain \
                                -Wl,--wrap,OS_SendSecureTCP -Wl,--wrap,OS_RecvSecureTCP -Wl,--wrap,close -Wl,--wrap,sqlite3_prepare_v2 \
                                -Wl,--wrap,sqlite3_bind_text -Wl,--wrap,sqlite3_bind_int -Wl,--wrap,sqlite3_step -Wl,--wrap,sqlite3_close_v2 \
                                -Wl,--wrap,sqlite3_finalize -Wl,--wrap,sqlite3_errmsg -Wl,--wrap,cJSON_CreateObject -Wl,--wrap,cJSON_CreateArray \
                                -Wl,--wrap,OPENSSL_init_ssl -Wl,--wrap,OPENSSL_init_crypto -Wl,--wrap,StartMQ -Wl,--wrap,SendMSG -Wl,--wrap,strerror \
                                -Wl,--wrap,_mterror_exit -Wl,--wrap,c_isdigit -Wl,--wrap,fopen -Wl,--wrap,fclose -Wl,--wrap,fgets -Wl,--wrap,fwrite \
                                -Wl,--wrap,OSRegex_Compile -Wl,--wrap,OSRegex_Execute")

list(APPEND vulndetector_names "test_wm_vuln_detector_nvd")
list(APPEND vulndetector_flags "-Wl,--wrap,_merror -Wl,--wrap,_mterror -Wl,--wrap,_mtdebug1 -Wl,--wrap,_mtdebug2 -Wl,--wrap,close -Wl,--wrap,sqlite3_prepare_v2 \
                                -Wl,--wrap,sqlite3_bind_text -Wl,--wrap,sqlite3_bind_int -Wl,--wrap,sqlite3_step -Wl,--wrap,sqlite3_close_v2 \
                                -Wl,--wrap,sqlite3_finalize -Wl,--wrap,sqlite3_errmsg -Wl,--wrap,strerror -Wl,--wrap,_mterror_exit -Wl,--wrap,sqlite3_column_int \
                                -Wl,--wrap,wm_checks_package_vulnerability -Wl,--wrap,sqlite3_column_text -Wl,--wrap,wm_vuldet_add_cve_node \
                                -Wl,--wrap,wm_vuldet_free_cve_node -Wl,--wrap,fopen -Wl,--wrap,fclose -Wl,--wrap,fgets -Wl,--wrap,fwrite \
                                -Wl,--wrap,OSRegex_Compile -Wl,--wrap,OSRegex_Execute")

# Compilig tests
list(LENGTH vulndetector_names count)
math(EXPR count "${count} - 1")
foreach(counter RANGE ${count})
    list(GET vulndetector_names ${counter} test_name)
    list(GET vulndetector_flags ${counter} test_flags)

    add_executable(${test_name} ${test_name}.c mocks_wm_vuln_detector.c)

    target_link_libraries(
        ${test_name}
        ${WAZUHLIB}
        ${WAZUHEXT}
        VULNDETECTOR_O
        ${TEST_DEPS}
    )

    if(NOT test_flags STREQUAL " ")
        target_link_libraries(
            ${test_name}
            ${test_flags}
        )
    endif()
    add_test(NAME ${test_name} COMMAND ${test_name})
endforeach()
